<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { guild_roomApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import { ElMessageBox } from 'element-plus';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:''
  })
  interface formQuert {
    room_id:number
    close_reason:string
  }
  const form = reactive({} as formQuert)
  const formRules = {
    close_reason: [{ required: true, trigger: 'blur', message: '请输入开启房间理由' }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      ElMessageBox.confirm(`确定开启房间${action.title}吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        const loading = Toastloading();
        guild_roomApi.addopenRoomRoomReason(form).then(()=>{
          ToastSuccess("操作成功!");
          window.location.reload();
          loading.close();
          closeDialog()
          $emit('updateList')
        }).catch(()=>{
          loading.close();
        })
      }).catch(() => {});

    })
  }
  const openDialog = (query:any = {})=> {
    const {
      room_id=0,
      room_uid=0,
      close_reason='',
      name
    } = query
    action.title = `${name}房间`
    Object.assign(form,{
      room_id,
      room_uid,
      close_reason
    })
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    Object.assign(form,{
      room_id:0,
      room_uid:0,
      close_reason:''
    })
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="xm-dialog input-width"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    width="60%"
    :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="开启原因" prop="close_reason">
          <el-input
            :disabled="action.disabled"
            v-model="form.close_reason"
            :autosize="{ minRows: 6, maxRows: 6}"
            type="textarea"
            placeholder="请输入开启房间原因"
          />
        </el-form-item>
      </el-form>

      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
